//
//  SHCourierQuotationListCell.m
//  youjian03
//
//  Created by Dadeso on 14/10/27.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHCourierQuotationListCell.h"
#import "UIImageView+WebCache.h"
#import "SHReturnToken.h"
// 支付宝头文件
#import "AlixPayOrder.h"
#import "PartnerConfig.h"
#import "DataSigner.h"
#import "AlixLibService.h"
// 支付宝头文件

@interface SHCourierQuotationListCell ()<UIAlertViewDelegate>

@property (assign,nonatomic)  NSInteger indexPathRow;

@property(nonatomic,copy) NSString *msg;//服务器返回的信息
@property(nonatomic,copy) NSString *deliverId;//订单id
@property(nonatomic,copy) NSString *price;//价格
@end

@implementation SHCourierQuotationListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIView * bgView = [[UIView alloc] init];
        bgView.frame = SHRectMake(0, 15, 320, 191);
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.cornerRadius = 5;
        [self addSubview:bgView];
        
        self.backgroundColor = [UIColor clearColor];
        
        UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
        imageView.frame = SHRectMakeRound(22, 7, 44, 44);
        imageView.layer.cornerRadius = imageView.bounds.size.width/2;
        imageView.layer.masksToBounds = YES;
        //imageView.backgroundColor = [UIColor blackColor];
        [bgView addSubview:imageView];
        self.headico = imageView;
        
        UILabel * label = [[UILabel alloc] initWithFrame:SHRectMake(83, 11, 100, 16)];
        label.text = @"大包子";
        label.font = [UIFont systemFontOfSize:15.0f];
        label.textColor = [SHUtilitiesTool colorWithHexString:@"#666666"];
        [bgView addSubview:label];
        self.name = label;
        
        UILabel * slabel = [[UILabel alloc] initWithFrame:SHRectMake(83, 32, 100, 16)];
        slabel.text = @"小包子";
        slabel.font = [UIFont systemFontOfSize:15.0f];
        slabel.textColor = [SHUtilitiesTool colorWithHexString:@"999999"];
        [bgView addSubview:slabel];
        self.companyName = slabel;
        
        UIImageView * starViewDown = [[UIImageView alloc] initWithFrame:SHRectMake(147, 14, 97, 26)];
        starViewDown.image = [UIImage imageNamed:@"1.png"];
        starViewDown.backgroundColor = [UIColor blackColor];
        starViewDown.hidden = YES;
        [bgView addSubview:starViewDown];
        
        UIImageView * starViewUp = [[UIImageView alloc] initWithFrame:SHRectMake(147, 14, 97, 26)];
        starViewUp.image = [UIImage imageNamed:@"1.png"];
        starViewUp.backgroundColor = [UIColor blackColor];
        starViewUp.hidden = YES;
        [bgView addSubview:starViewUp];
        self.starUp = starViewUp;
        
        UILabel * ssLabel = [[UILabel alloc] initWithFrame:SHRectMake(240, 11, 77, 13)];
        ssLabel.text = @"1分钟以前";
        ssLabel.font = [UIFont systemFontOfSize:12.0f];
        ssLabel.textColor = [SHUtilitiesTool colorWithHexString:@"#cccccc"];
        [bgView addSubview:ssLabel];
        self.offerTime = ssLabel;
        
        UIView * viewUp = [[UIView alloc] initWithFrame:SHRectMake(0, 58, 320, 1)];
        viewUp.backgroundColor = [UIColor lightGrayColor];
        [bgView addSubview:viewUp];
        
        //        MLPlayVoiceButton * voiceBtn = [[MLPlayVoiceButton alloc] init];
        //        [voiceBtn setBackgroundImage:[UIImage imageNamed:@"audio_play_animation3"] forState:UIControlStateNormal];
        //        [voiceBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        //        voiceBtn.hidden = YES;
        //        voiceBtn.tag = ButtonTagPlayVoiceTypeOne;
        //        voiceBtn.frame = CGRectMake(128, 71, 65, 65);
        //        self.playVoiceTypeOneImage = voiceBtn;
        //        self.playVoiceTypeOneImage.hidden = YES;
        //        [self addSubview: self.playVoiceTypeOneImage];

        MLPlayVoiceButton * voiceBtn = [[MLPlayVoiceButton alloc] init];
        voiceBtn.frame = SHRectMakeRound(139, 82, 42, 42);
        //[voiceBtn setImage:[UIImage imageNamed:@"audio_played_84x84"] forState:UIControlStateNormal];
        self.playVoiceTypeOneImage = voiceBtn;
        [bgView addSubview:self.playVoiceTypeOneImage];
        
        //        UIButton * voiceBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        //        [voiceBtn1 setBackgroundImage:[UIImage imageNamed:@"audio_play_animation3"] forState:UIControlStateNormal];
        //        [voiceBtn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        //        voiceBtn1.hidden = YES;
        //        voiceBtn1.tag = ButtonTagPlayVoiceTypeTwo;
        //        voiceBtn1.frame = CGRectMake(60, 71, 65, 65);
        //        self.playVoiceTypeTwoImage = voiceBtn1;
        //        self.playVoiceTypeTwoImage.hidden = YES;
        //        [self addSubview: self.playVoiceTypeTwoImage];
        MLPlayVoiceButton * voiceBtn1 = [[MLPlayVoiceButton alloc] init];
        voiceBtn1.frame = SHRectMakeRound(70, 82, 42, 42);
        //[voiceBtn1 setImage:[UIImage imageNamed:@"audio_played_84x84"] forState:UIControlStateNormal];
        self.playVoiceTypeTwoImage = voiceBtn1;
        [bgView addSubview:self.playVoiceTypeTwoImage];
        
        
        UIButton * voiceBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [voiceBtn2 setImage:[UIImage imageNamed:@"tel_normal"] forState:UIControlStateNormal];
        [voiceBtn2 setImage:[UIImage imageNamed:@"tel_highlighted"] forState:UIControlStateHighlighted];
        [voiceBtn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        voiceBtn2.hidden = YES;
        voiceBtn2.tag = ButtonTagPlayCall;
        voiceBtn2.frame = SHRectMakeRound(205, 82, 42, 42);
        self.playPhoneTypeTwoImage = voiceBtn2;
        self.playPhoneTypeTwoImage.hidden = YES;
        [bgView addSubview: self.playPhoneTypeTwoImage];
        
        UIView * viewDown = [[UIView alloc] initWithFrame:SHRectMake(0, 145, 320, 1)];
        viewDown.backgroundColor = [UIColor lightGrayColor];
        [bgView addSubview:viewDown];
        
        UIButton * btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        btnLeft.frame = SHRectMake(0, 146, 160, 44);
        btnLeft.tag = ButtonTagLeft;
        [btnLeft setTitle:@"同意" forState:UIControlStateNormal];
        [btnLeft setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [btnLeft addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:btnLeft];
        self.leftOfferButton = btnLeft;
        
        UIView * viewMid = [[UIView alloc] initWithFrame:SHRectMake(160, 146, 1, 44)];
        viewMid.backgroundColor = [UIColor lightGrayColor];
        [bgView addSubview:viewMid];
        self.midLineView = viewMid;
        
        UIButton * btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        btnRight.frame = SHRectMake(161, 146, 159, 44);
        btnRight.tag = ButtonTagRight;
        [btnRight setTitle:@"拒绝" forState:UIControlStateNormal];
        [btnRight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btnRight addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:btnRight];
        self.rightOfferButton = btnRight;
    }
    return self;
}

//-(id)initWithFrame:(CGRect)frame{
//    if (self = [super initWithFrame:CGRectMake(10, 15, frame.size.width - 20, frame.size.height - 20)]) {
//        
//    }
//    return self;
//}


-(void)loadCell:(NSMutableArray *)dataModelArray IndexPath:(NSInteger) indexPathV cellType:(NSString *) loadCellType{
    SHCourierQuotationListModel * dataModel = [[SHCourierQuotationListModel alloc] init];
    dataModel = (SHCourierQuotationListModel *)[dataModelArray objectAtIndex:indexPathV];
    self.cellType = loadCellType;
    self.indexPathRow = indexPathV;
    self.cellDataModel = dataModel;
    self.deliverId =  dataModel.courierOfferId ;
    
    NSString * audioString = [[dataModel.courierAudio componentsSeparatedByString:@"/"] lastObject];
    if ([self.cellType isEqualToString:ORDER_STATE_RECIEVE_VOICE]) {
        self.playVoiceTypeOneImage.hidden = NO;
        self.playVoiceTypeTwoImage.hidden = YES;
        self.playPhoneTypeTwoImage.hidden = YES;
        //TODO: 添加语音路径
        [self.playVoiceTypeOneImage setVoiceWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@audio/%@?download",SHAPIURL_VOICE_B,audioString]]];
        //[self.playVoiceTypeOneImage setImage:[UIImage imageNamed:@"audio_played_84x84"] forState:UIControlStateNormal];
        self.name.text = dataModel.courierName;
        self.companyName.text = dataModel.courierCompanyName;
    
        if ([dataModel.courierHeadico hasSuffix:@"\n"]) {
            NSString * headicoString = [dataModel.courierHeadico stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            [self.headico setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SHAPIURL_PHOTO_B,headicoString]]
                         placeholderImage:[UIImage imageNamed:@"pic_profile"]];
        }else{
            
            [self.headico setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SHAPIURL_PHOTO_B,dataModel.courierHeadico]]
                         placeholderImage:[UIImage imageNamed:@"pic_profile"]];
        }
        NSDate * date2 = [NSString longtotime:[NSString stringWithFormat:@"%@",dataModel.courierOfferTime]];
        self.offerTime.text = [SHUtilitiesTool createdWithTime:date2];
    }else if ([self.cellType isEqualToString:ORDER_STATE_WAIT_COURIER]){
        self.playVoiceTypeOneImage.hidden = YES;
        self.playVoiceTypeTwoImage.hidden = NO;
        self.playPhoneTypeTwoImage.hidden = NO;
        
        [self.rightOfferButton setTitle:@"现金支付" forState:UIControlStateNormal];
        [self.leftOfferButton setTitle:@"在线支付" forState:UIControlStateNormal];

        
        [self.leftOfferButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        //self.midLineView.hidden = YES;
        //self.leftOfferButton.hidden = YES;
        [self.playVoiceTypeTwoImage setVoiceWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@audio/%@?download",SHAPIURL_VOICE_B,audioString]]];
        self.name.text = dataModel.courierName;
        //[self.playVoiceTypeTwoImage setImage:[UIImage imageNamed:@"audio_played_84x84"] forState:UIControlStateNormal];
        self.companyName.text = dataModel.courierCompanyName;
        NSDate * date2 = [NSString longtotime:[NSString stringWithFormat:@"%@",dataModel.courierOfferTime]];
        self.offerTime.text = [SHUtilitiesTool createdWithTime:date2];
        
    }
}

-(void)btnClick:(UIButton *) btn{
    if (btn.tag == ButtonTagLeft) {
        if ([self.cellType isEqualToString:ORDER_STATE_RECIEVE_VOICE]) {
            [_delegate makeSureZheCourier:self.indexPathRow];
        }else if ([self.cellType isEqualToString:ORDER_STATE_WAIT_COURIER]){

            
            if (IOS8)
            {
                NSString *token = [SHReturnToken returnTokenNo];
                [_delegate onlinePayWithToken:token anddeliverId:self.deliverId];

            }
            else//IOS7
            {
                
 
 
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付订单"  message:@"请输入金额" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认",nil];

                alert.alertViewStyle = UIAlertViewStylePlainTextInput;
                [alert textFieldAtIndex:0] .keyboardType =UIKeyboardTypePhonePad;// UIKeyboardTypeNumberPad;
                [alert show];
                
                
                
 
            }
             //---------------------------------------------------------------------------------------

            
            
            
        }
    }else if(btn.tag == ButtonTagRight){
        if ([self.cellType isEqualToString:ORDER_STATE_RECIEVE_VOICE]) {
            [_delegate refuseZheCourier:self.indexPathRow];
        }else if ([self.cellType isEqualToString:ORDER_STATE_WAIT_COURIER]){
            [_delegate payCashMoney];
            
            
            
            
            
        }
    }else if(btn.tag == ButtonTagPlayVoiceTypeOne){
        
    }else if(btn.tag == ButtonTagPlayVoiceTypeTwo){
        
    }else if(btn.tag == ButtonTagPlayCall){
        //打电话
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.cellDataModel.courierPhone];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self addSubview:callWebview];
        
        /*
         NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"186xxxx6979"];
         //            NSLog(@"str======%@",str);
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]
         */
    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.price =  [alertView textFieldAtIndex:0].text;
    NSString *token = [SHReturnToken returnTokenNo];
    //TODO: 价格
    
    NSDictionary *dic =@{@"deliverId":self.deliverId,@"token":token,@"price":@"0.01"};//self.price};
    HMLog(@"dic =%@",dic);
    [SHHTTPTOOL PostOrderWithDic:dic andAction:@"userPayOrder" success:^(id responseObj) {


        NSDictionary * dict = [SHUtilitiesTool dictionaryWithJsonString:responseObj];
                HMLog(@"%@",dict);
        NSString *str = ((NSDictionary *)dict[@"msg"])[@"sign"];
        
        NSString *orderString =str;
        
        
        
        // 4.打开支付宝,传递订单信息
        [AlixLibService payOrder:orderString AndScheme:@"youjian" seletor:@selector(paymentResultDelegate:) target:self];
        
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

-(void)playVoiceCount{
    
    
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
